package com.briup.cms.web.controller;

import com.briup.cms.bean.BaseConfig;
import com.briup.cms.service.IBaseConfigService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "系统配置")
@RestController
@RequestMapping("/auth/config")
public class ConfigController {
    @Autowired
    private IBaseConfigService service;

    @ApiOperation(value = "保存配置信息")
    @PostMapping({"/save"})
    public Message saveConfig(BaseConfig config){
        service.saveConfig(config);
        return MessageUtil.success("保存成功");
    }

    @ApiOperation(value = "更新配置")
    @PutMapping({"/update"})
    public Message updateConfig(BaseConfig config){
        service.updateConfig(config);
        return MessageUtil.success("更新成功");
    }


    @ApiOperation(value = "改变配置信息的状态")
    @PutMapping("/update/status")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "主键",paramType = "query",required = true),
                        @ApiImplicitParam(name = "status",value = "状态，0-禁用，1-启用",paramType = "query",required = true)})
    public Message changeConfigStatus(int id, int status){
        service.changeConfigStatus(id,status);
        return MessageUtil.success("修改成功");
    }

    @ApiOperation(value = "通过id删除配置信息")
    @DeleteMapping("/deleteById")
    @ApiImplicitParams(@ApiImplicitParam(name = "id",value = "主键",paramType = "query",required = true))
    public Message deleteById(int id){
        service.deleteById(id);
        return  MessageUtil.success("删除成功");
    }

    @ApiOperation(value = "查询所有配置信息")
    @GetMapping("/page/query")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum",value = "当前页",paramType = "query",required = true),
                        @ApiImplicitParam(name = "pageSize",value = "每页大小",paramType = "query",required = true)})
    public Message findAllWithPage(int pageNum, int pageSize){
        PageInfo<BaseConfig> list =  service.findAllWithPage(pageNum,pageSize);
        return MessageUtil.success(list);
    }

    @ApiOperation(value = "查找当前启用的系统配置")
    @GetMapping("/current/config/yes")
    public Message currentConfigYes(){
        BaseConfig config = service.currentConfigYes();
        return MessageUtil.success(config);
    }


}
